package Dao;

import Vo.Comment;

import java.sql.*;
import java.util.HashMap;
import java.util.Random;

public class CommentDao {
    private Connection conn = null;
    public void initConnection() throws Exception {
        //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Class.forName("com.mysql.cj.jdbc.Driver");
        //conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=school", "sa", "123456");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/forum", "root", "root");
    }
    public void closeConnection() throws Exception {
        conn.close();
    }
    public HashMap getallcommentbyPid(String Pid)throws Exception {
        HashMap hm = new HashMap();
        this.initConnection();
        String sql = "SELECT Cid,Pid,authorID,contents,createtime,ownerID,username FROM comments join user on comments.ownerID=user.Uid where Pid=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, Pid);
        ResultSet resultSet=ps.executeQuery();
        while (resultSet.next()){
            Comment comment=new Comment();
            comment.setCid(resultSet.getString("Cid"));
            comment.setPid(resultSet.getString("Pid"));
            comment.setAuthorID(resultSet.getString("authorID"));
            comment.setContents(resultSet.getString("contents"));
            comment.setCreatetime(resultSet.getString("createtime"));
            comment.setOwnerID(resultSet.getString("ownerID"));
            comment.setUsername(resultSet.getString("username"));
//            System.out.println("Cid:"+comment.getCid());
//            System.out.println("Pid:"+comment.getPid());
//            System.out.println("AuthorID:"+comment.getAuthorID());
//            System.out.println("Contents:"+comment.getContents());
//            System.out.println("Createtime:"+comment.getCreatetime());
//            System.out.println("OwnerId:"+comment.getOwnerID());
//            System.out.println("username:"+comment.getUsername());
            hm.put(comment.getCid(),comment);
        }
        closeConnection();
        return hm;
    }
    public Comment getCommentbyCid(String Cid) throws Exception {
        Comment comment=new Comment();
        initConnection();
        String sql = "SELECT Cid,Pid,authorID,contents,createtime,ownerID FROM comments join user on comments.ownerID=user.Uid WHERE Cid=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, Cid);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            comment.setCid(rs.getString("Cid"));
            comment.setPid(rs.getString("Pid"));
            comment.setAuthorID(rs.getString("authorID"));
            comment.setContents(rs.getString("contents"));
            comment.setCreatetime(rs.getString("createtime"));
            comment.setOwnerID(rs.getString("ownerID"));
            comment.setUsername(rs.getString("username"));
//			System.out.println("uid:"+user.getUid());
//			System.out.println("password:"+user.getPassword());
//			System.out.println("username:"+user.getUsername());
        }
        closeConnection();
        return comment;
    }
    public void Comment(String Pid,String authorID,String contents,String ownerID)throws Exception{
        initConnection();
        System.out.println("数据库authorID:"+authorID);
        System.out.println("数据库Pid:"+Pid);
        System.out.println(contents);
        java.util.Date  date=new java.util.Date();
        java.sql.Date  sqldate=new java.sql.Date(date.getTime());
        String sql="insert into comments(Cid,Pid,authorID,contents,createtime,ownerID) values(?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,Integer.toString(new Random().nextInt(1000)+2000));
        ps.setString(2,Pid);
        ps.setString(3,authorID);
        ps.setString(4,contents);
        ps.setDate(5,sqldate);
        ps.setString(6,ownerID);
        ps.executeUpdate();


    }
}
